﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Xml.Linq;
using AMS.Business;
using System.Net;
using System.IO;
using System.Text;

namespace AMS.WebApp
{
    public partial class Default
    {
        private readonly AccountInfoBLL accountBll = new AccountInfoBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            //DataTable dt = accountBll.GetAllAccountInfo();
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            string gethost = string.Empty;
            CookieContainer cc = new CookieContainer();
            string Cookiesstr = string.Empty;
            try
            {

                //第一次POST请求

                string postdata = "__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKLTM0NjMxMjc3MQ9kFgICAw9kFgICAQ8PZBYCHglvbmtleWRvd24FG2NsZWFyRGVmYXVsdFRleHRCeUtleWRvd24oKWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFD0NoZWNrQm94U2F2ZVB3ZAULQnV0dG9uTG9naW64lXNq9UajkIsvdWwEvNpBXUrvng%3D%3D&__EVENTVALIDATION=%2FwEWBgLN6%2FrUDwLut%2B2uDgKpzpH0DQK12taIBQKWrKCPDwLK8b%2BXDcx1xvzlnmlKWtUhvCBbTJt8DBxL&TextBoxEmployeeNo=itdc&TextBoxPassword=ITDC%40Admin&ButtonLogin.x=112&ButtonLogin.y=17";
                string LoginUrl = "http://travel.hisoft.com/TravellingLogin.aspx";


                request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类
                request.Method = "POST";//数据提交方式为POST
                //模拟头
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = postdatabytes.Length;
                //request.Referer = "http://www.renren.com/Login.do?rf=r&domain=renren.com&origURL=" + HostUrl;
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                //提交请求
                Stream stream;
                stream = request.GetRequestStream();
                stream.Write(postdatabytes, 0, postdatabytes.Length);
                stream.Close();
                //接收响应
                response = (HttpWebResponse)request.GetResponse();
                //保存返回cookie
                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
                CookieCollection cook = response.Cookies;
                string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
                Cookiesstr = strcrook;
                //取第一次GET跳转地址
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string content = sr.ReadToEnd();
                response.Close();
                string[] substr = content.Split(new char[] { '"' });

                gethost = "http://travel.hisoft.com/MyAudit.aspx";
            }
            catch (Exception)
            {
                //第一次POST出错；
            }
            try
            {
                request = (HttpWebRequest)WebRequest.Create(gethost);
                request.Method = "GET";
                request.KeepAlive = true;
                request.Headers.Add("Cookie:" + Cookiesstr);
                request.CookieContainer = cc;
                request.AllowAutoRedirect = false;
                response = (HttpWebResponse)request.GetResponse();
                //设置cookie
                Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
                //取再次跳转链接
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string ss = sr.ReadToEnd();
                string[] substr = ss.Split(new char[] { '"' });
                gethost = substr[1];
                request.Abort();
                sr.Close();
                response.Close();
            }
            catch (Exception)
            {
                //第一次GET出错
            }
            try
            {
                //第二次GET请求
                request = (HttpWebRequest)WebRequest.Create(gethost);
                request.Method = "GET";
                request.KeepAlive = true;
                request.Headers.Add("Cookie:" + Cookiesstr);
                request.CookieContainer = cc;
                request.AllowAutoRedirect = false;
                response = (HttpWebResponse)request.GetResponse();
                //设置cookie
                Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
                request.Abort();
                response.Close();
            }
            catch (Exception)
            {
                //第二次GET出错
            }

        }
    }
}
